//index.js
let qqmapsdk = require('../../utils/qqmap-wx-jssdk.js');
let drawQrcode = require('../../utils/wxqrcode.js');
var mapsdk = new qqmapsdk({ key: '3AOBZ-MUP6X-WDI4H-75FQP-QY3NS-YQFJ3' })
var app = getApp()
Page({
  data: {
    // 用户类型
    userType: 0,
    userPageType: 1,
    // 订单状态
    statusList: ['未付款', '待商家确认', '待使用', '已完成', '已取消'],
    arrlist: [],
    //附近的营地
    Camplist: [],
    // 营地列表数据
    AllCamplist: [],
    background: [],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    searchIconToggle: false,
    searchVal: '',
    // pageType: 2,

    statusBarHeight: app.globalData.statusBarHeight,
    pageType: 1,

    // index
    userInfo: null,
    open: false,   //是否打开红包
    page: true,   //红包是否显示 
    orderOrBusiness: 'order',
    autoplay: true,
    interval: 3000,
    duration: 500,
    vertical: true,
    circular: true,

    height: 0,
    buycar_num: 0,
    block: false,  //选规格
    bindId: 0,

    cityName: '',
    map_address: '',

    quickScan: false,
    outCheckNo: '',

    // buycar
    totalMoney: 0,
    chooseAll: false,
    // order
    orderOk: false,
    orderCancel: false,
    orderList: [],
    ifOrderCheckEmpty: true,
    orderCheck: false,
    // me
    img: '../../img/qishou.png',
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    showAuth: false,
  },
  onLoad: function () {
    var that = this
    console.log(app.globalData)
    // this.getIp()
    this.getCarouselIndex()
    this.getCampDataList()
    this.getLocalCamp()
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo
      })
      this.getAllddList()
    } else {
      wx.getUserInfo({
        success: res => {
          console.log(res)
          this.getUserInfo(res)
          // 可以将 res 发送给后台解码出 unionId
          this.setData({
            userInfo: res.userInfo
          })
          this.getBindAccount()
        },
        fail: err => {
          console.log(err)
        }
      })
      // wx.showLoading({
      //   title: '登录中',
      //   mask: true
      // })
      // wx.getSetting({
      //   success: res => {
      //     if (res.authSetting['scope.userInfo']) {
      //       wx.getUserInfo({
      //         success: res => {
      //           wx.hideLoading()
      //           this.getUserInfo(res)
      //           this.setData({
      //             userInfo: res.userInfo
      //           })
      //           this.getBindAccount()
      //         }
      //       })
      //     }
      //   }
      // })
    }

    var that = this
    // 微信获取用户地理位置授权
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        // 获取当前地理位置
        mapsdk.reverseGeocoder({
          location:{
            latitude: latitude,
            longitude: longitude
          },
          success: function(res) {
            // 判断返回数据是否正确
            if (res.status === 0) {
              var address = res.result.address_component;
              that.setData({
                map_address: address.city + '/'+address.district,
                cityName: address.city
              })
            }
            console.log('城市信息获取成功', res)
          },
          fail: function(err) {
            console.log('城市信息获取失败', err)
          }
        })
      },
      fail: function(err) {
        console.log('地理位置获取失败', err)
      }
    })
  },
  // 微信授权
  bindGetUserInfo(e) {
    if (e.detail.userInfo) {
      wx.showLoading({
        title: '获取授权中',
        mask: true
      })
      wx.getSetting({
        success: res => {
          //用户按了允许授权按钮
          wx.getUserInfo({
            success: res => {
              this.getUserInfo(res)
              // 可以将 res 发送给后台解码出 unionId
              this.setData({
                userInfo: res.userInfo
              })
              this.getBindAccount()
            }
          })
        }
      })
    }
  },
  // 微信授权 -> 接口访问是否已绑定终端账号
  getBindAccount() {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, userId
        wx.request({
          url: `${app.globalData.testApiUrl}/api/wx/user/${app.globalData.appId}/login/${res.code}`,
          success: res => {
            wx.hideLoading()
            wx.showToast({
              title: '授权成功',
              icon: 'succes',
              duration: 1000,
              mask: true
            })
            app.globalData.openId = res.data.openId
            app.globalData.sessionKey = res.data.sessionKey
            wx.request({
              url: `${app.globalData.testApiUrl}/api/wx/login/login`,
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                avatarUrl: app.globalData.userInfo.avatarUrl,
                city: app.globalData.userInfo.city,
                gender: '' + app.globalData.userInfo.gender,
                nickName: app.globalData.userInfo.nickName,
                openid: app.globalData.openId
              },
              success: (res) => {
                console.log('登录成功', res)
                app.globalData.userId = res.data.id
                app.globalData.userType = res.data.usertype
                this.setData({
                  userType: res.data.usertype,
                  // userType: 1
                })
                this.getAllddList()
              },
              fail: (error) => {
                console.log('登录失败', error)
              }
            })
          },
          complete: function (e) {
            app.errorDate.backData = e.data
            app.errorDate.statusCode = e.statusCode
            app.errorDate.route = 'index'
            app.errorDate.interface = '/api/wx/login/login'
            app.setLogCloud()
            wx.hideLoading()
          }
        })
      }
    })
  },
  getUserInfo (e) {
    app.globalData.userInfo = e.userInfo
  },
  // 获取首页轮播图
  getCarouselIndex () {
    wx.request({
      url:  `${app.globalData.testApiUrl}/api/wx/base/indexPic`,
      success: (res) => {
        for (var i in res.data) {
          res.data[i].filterImg = app.globalData.testApiUrl + res.data[i].picPath
        }
        this.setData({
          background: res.data
        })
      },
      fail: (error) => {
        console.log('接口调用失败', error)
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/base/indexPic'
        app.setLogCloud()
      }
    })
  },
  // 获取ip地址
  getIp () {
    var that = this
    wx.request({  // 获取ip
      url: 'http://ip-api.com/json',
      success: function (e) {
        // that.setData({
        //   motto: e.data.query
        // })
        app.globalData.ip = e.data.query
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = 'http://ip-api.com/json'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  // 获取附近营地信息
  getLocalCamp: function () {
    var that = this
    wx.showLoading({
      title: '营地列表获取中',
      mask: true
    })
    wx.request({
      url: `${app.globalData.testApiUrl}/api/wx/camp/list`,
      success: res => {
        // console.log('营地信息', res)
        // 处理返回的营地信息
        if (res.statusCode === 200) {
          var dataList = []
          // 循环列表获取，营地名称和描述，地理位置等信息
          for (var i in res.data) {
            // 判断描述字符串长度是否超过30
            if (res.data[i].campDesc) {
              if (res.data[i].campDesc.length > 15) {
                dataList.push({
                  img: app.globalData.testApiUrl + res.data[i].campPic,
                  name: res.data[i].pathName,
                  describe: res.data[i].campDesc,
                  subdescribe: res.data[i].campDesc.substr(0,15),
                  campid: res.data[i].id,
                  address: res.data[i].address,
                })
              } else {
                dataList.push({
                  img: app.globalData.testApiUrl + res.data[i].campPic,
                  name: res.data[i].pathName,
                  describe: res.data[i].campDesc,
                  address: res.data[i].address,
                  campid: res.data[i].id,
                })
              }
            } else {
              dataList.push({
                img: app.globalData.testApiUrl + res.data[i].campPic,
                name: res.data[i].pathName,
                describe: res.data[i].campDesc,
                address: res.data[i].address,
                campid: res.data[i].id,
              })
            }
          }
          // console.log(dataList)
          // 把处理好的值赋给变量
          that.setData({
            Camplist: dataList
          })
          // console.log('营地信息', res, that.data.AllCamplist)
        }
      },
      fail: error => {
        wx.hideLoading()
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/camp/list'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
 // 获取所有订单信息
  getAllddList: function () {
    var that = this
    wx.showLoading({
      title: '订单列表获取中',
      mask: true
    })
    // 调用订单获取接口
    wx.request({
      url:  `${app.globalData.testApiUrl}/api/wx/order/listOrder`,
      data: {
        userId : app.globalData.userId
      },
      success: (res) => {
        // console.log('接口调用成功', res)
        var datalist = []
        for (let i in res.data) {
          datalist.push({
            orderStatus: res.data[i].status,
            orderno: res.data[i].id,
            pay: res.data[i].price,
            outTradeNo: res.data[i].orderNo,
            remark: res.data[i].remark,
            campName: res.data[i].camp.pathName,
            tenttype: res.data[i].tenttype,
            tenttypeImg: res.data[i].tenttype.typePic ? app.globalData.testApiUrl + res.data[i].tenttype.typePic : '',
            checkNo: res.data[i].checkNo ? res.data[i].checkNo : ''
            // produce: [{ name: res.data[i].tentType, num: 1, price: res.data[i].price }]
          })
        }
        that.setData({
          orderList:datalist
        })
        wx.hideLoading()
      },
      fail: (error) => {
        console.log('接口调用失败', error)
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/order/listOrder'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  // 获取营地列表
  getCampDataList: function () {
    var that = this
    wx.showLoading({
      title: '营地列表获取中',
      mask: true
    })
    // 调用数据获取接口
    wx.request({
      url: `${app.globalData.testApiUrl}/api/wx/camp/list`,
      success: res => {
        // console.log('营地信息', res)
        // 处理返回的营地信息
        if (res.statusCode === 200) {
          var dataList = []
          // 循环列表获取，营地名称和描述，地理位置等信息
          for (var i in res.data) {
            // 判断描述字符串长度是否超过30
            if (res.data[i].campDesc){
              if (res.data[i].campDesc.length > 29) {
                dataList.push({
                  img: app.globalData.testApiUrl+ res.data[i].campPic,
                  name: res.data[i].pathName,
                  describe: res.data[i].campDesc,
                  subdescribe: res.data[i].campDesc.substr(0,30),
                  campid: res.data[i].id,
                  address: res.data[i].address,
                })
              } else {
                dataList.push({
                  img: app.globalData.testApiUrl+ res.data[i].campPic,
                  name: res.data[i].pathName,
                  describe: res.data[i].campDesc,
                  address: res.data[i].address,
                  campid: res.data[i].id,
                })
              }
            } else {
              dataList.push({
                img: app.globalData.testApiUrl+ res.data[i].campPic,
                name: res.data[i].pathName,
                describe: res.data[i].campDesc,
                address: res.data[i].address,
                campid: res.data[i].id,
              })
            }
          }
          // console.log(dataList)
          // 把处理好的值赋给变量
          that.setData({
            AllCamplist: dataList
          })
          // console.log('营地信息', res, that.data.AllCamplist)
        }
      },
      fail: error => {
        wx.hideLoading()
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/camp/list'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  // tab切换
  turnMenu: function(e) {
    var type = e.target.dataset.index;
    this.setData({
      orderType: type
    })
  },
  // tab
  chooseType: function (e) {
    var type = e.currentTarget.dataset.id;
    if (type == 1 && this.data.restaurant != true) {
      wx.setNavigationBarTitle({ title: '首页' })
    } else if (type == 2) {
      wx.setNavigationBarTitle({ title: '营地' })
    } else if (type == 3) {
      wx.setNavigationBarTitle({ title: '订单' })
    } else if (type == 4) {
      wx.setNavigationBarTitle({ title: '我的' })
    }
    this.setData({
      pageType: type
    })
  },
  // 营地端导航栏的点击事件
  userChooseType: function (e) {
    var type = e.currentTarget.dataset.id;
    if (type == 1){
      wx.setNavigationBarTitle({ title: '统计' })
    } else if (type == 2) {
      wx.setNavigationBarTitle({ title: '我的' })
    }
    this.setData({
      userPageType: type
    })
  },
  // index
  searchKey: function(e) {
    this.setData({
      searchKey: e.detail.value
    })    
  },
  // 验证按钮
  searchBtn: function() {
    var keyWork = this.data.searchKey;
    wx.redirectTo({
      url: '../test/test',
    })
    wx.request({
      url: '',
      data: {
        
      },
      success: function (res) {
        
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/test/test'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  // tab切换
  tabChange: function(e) {
    var type = e.currentTarget.dataset.id;
    this.setData({
      orderOrBusiness: type
    })
  },
  //打开红包
  getPag: function() {
    this.setData({
      open: true,
      page: false
    })
  },
  //关闭红包
  hasGet: function () {
    this.setData({
      open: false
    })
  },
  reduce: function (e) {
    var i = e.currentTarget.dataset.id;
    var arr3 = this.data.arr3;
    if (arr3[i].num > 1) { 
      wx.showToast({
        title: '多规格商品只能去购物车删除哦',
        duration: 2000
      })
    }else{
      if (arr3[i].num > 0) {
        arr3[i].num = parseInt(arr3[i].num) - 1;
        this.setData({
          arr3: arr3
        })
      }      
    }
  },
  reduce2: function (e) {
    var i = e.currentTarget.dataset.id;
    var arr3 = this.data.arr3;
    if (arr3[i].num > 0) {      
      arr3[i].num = parseInt(arr3[i].num) - 1;
      this.setData({
        arr3: arr3
      })
    }
  },
  add: function (e) {
    var i = e.currentTarget.dataset.id;
    var arr3 = this.data.arr3;
    var arr = arr3[i].message3;    
    this.setData({
      block: true,
      arr2: arr,
      bindId: i
    })
  },
  add2: function (e) {
    var i = e.currentTarget.dataset.id;
    var arr3 = this.data.arr3;
    arr3[i].num = parseInt(arr3[i].num) + 1;
    this.setData({
      arr3: arr3
    })
  },
  close: function () {
    this.setData({
      block: false
    })
  },
  resetNum: function (e) {
    var type = e.currentTarget.dataset.id;
    this.setData({
      foodtype: type
    })
  },
  submit: function() {
    var i = this.data.bindId;
    var arr3 = this.data.arr3;
    arr3[i].num = parseInt(arr3[i].num)+1
    this.setData({
      block: false,
      arr3: arr3
    })
  },
  getAddress: function() {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        if (res.address.length > 10) {
          res.address = res.address.substr(0, 10) + '...'
        }
        that.setData({
          map_address: res.address
        })
      },
    })
  },
  // buycar
  del: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除吗',
      success: function (res) {
        if (res.confirm) {
          var index = parseInt(e.currentTarget.dataset.id);
          var arr = that.data.arr;
          var totalMoney = that.data.totalMoney;
          var buycar_num = that.data.buycar_num;
          if (arr[index].selected) {
            totalMoney -= Number(arr[index].price * arr[index].num);
            buycar_num -= Number(arr[index].num);
          }
          arr.splice(index, 1);
          that.setData({
            arr: arr,
            totalMoney: totalMoney,
            buycar_num: buycar_num
          });
        }
      }
    })    
  },
  choose: function (e) {
    var index = parseInt(e.currentTarget.dataset.id);
    var selected = this.data.arr[index].selected;
    var arr = this.data.arr;
    var totalMoney = this.data.totalMoney;
    var buycar_num = this.data.buycar_num;
    arr[index].selected = !selected;
    if (arr[index].selected) {
      totalMoney += Number(arr[index].price * arr[index].num);
      buycar_num += Number(arr[index].num);
    } else {
      totalMoney -= Number(arr[index].price * arr[index].num);
      buycar_num -= Number(arr[index].num);
    }
    totalMoney = Number(totalMoney).toFixed(2);
    this.setData({
      arr: arr,
      totalMoney: Number(totalMoney),
      buycar_num: buycar_num
    });
  },
  // 提交页
  toSubmit: function() {
    wx.navigateTo({
      url: '../submitOrder/submitOrder',
    })
  },
  numAdd: function(e) {
    var index = parseInt(e.currentTarget.dataset.id);
    var selected = this.data.arr[index].selected;
    var arr = this.data.arr;
    var totalMoney = this.data.totalMoney;
    var buycar_num = this.data.buycar_num;
    arr[index].num = Number(arr[index].num) + 1;
    if(selected==true){
      totalMoney += Number(arr[index].price);
      buycar_num += 1;
    }
    totalMoney = Number(totalMoney).toFixed(2);
    this.setData({
      arr: arr,
      totalMoney: Number(totalMoney),
      buycar_num: buycar_num
    });
  },
  numReduce: function (e) {
    var index = parseInt(e.currentTarget.dataset.id);    
    var arr = this.data.arr;
    if (arr[index].num > 1) {
      var selected = arr[index].selected;
      var totalMoney = this.data.totalMoney;
      var buycar_num = this.data.buycar_num;
      arr[index].num = Number(arr[index].num) - 1;
      if (selected == true) {
        totalMoney -= Number(arr[index].price);
        buycar_num -= 1;
      }
      totalMoney = Number(totalMoney).toFixed(2);
      this.setData({
        arr: arr,
        totalMoney: Number(totalMoney),
        buycar_num: buycar_num
      });
    }    
  },
  // order
  orderOk: function() {
    this.setData({
      orderOk: true
    })
  },
  orderCancel: function (e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗？',
      success (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '取消订单中',
            mask: true
          })
          wx.request({
            url:  `${app.globalData.testApiUrl}/api/wx/order/deleteOrder`,
            data: {
              orderId: e.currentTarget.dataset.id
            },
            success: (res) => {
              wx.hideLoading()
              wx.showToast({
                title: '取消成功',
                duration: 2000
              })
              setTimeout(() => {
                that.getAllddList()
              }, 2000)
            },
            fail: (error) => {
              console.log('接口调用失败', error)
            },
            complete: function (e) {
              app.errorDate.backData = e.data
              app.errorDate.statusCode = e.statusCode
              app.errorDate.route = 'index'
              app.errorDate.interface = '/api/wx/order/deleteOrder'
              app.setLogCloud()
              wx.hideLoading()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  okCancel: function() {
    this.setData({
      orderOk: false
    })
  },
  cancelCancel: function () {
    this.setData({
      orderCancel: false
    })
  },
  // 退出
  logout: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.setData({
            userInfo: null,
            orderList: []
          })
          app.globalData.userInfo = null
          app.globalData.userId = ''
          app.globalData.userType = ''
          app.globalData.openId = ''
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  okOk: function () {
    this.setData({
      orderOk: false
    })
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toMyPackage: function () {
    wx.navigateTo({
      url: '../myPackage/myPackage'
    })
  },
  toDetail: function (event) {
    wx.navigateTo({
      url: '../orderDetail/orderDetail?status=' + event.currentTarget.dataset.status+'&orderno='+event.currentTarget.dataset.orderno
    })
  },
  // 订单列表支付
  orderPay: function (e) {
    var index = e.currentTarget.dataset.index
    var that = this
    var params = {
      appid: app.globalData.appId,
      openid: app.globalData.openId,
      nonceStr: app.randomWord(),
      notifyUrl: `${app.globalData.testApiUrl}/api/wx/pay/notify/order`,
      body: that.data.orderList[index].remark,
      totalFee: that.data.orderList[index].pay * 100,
      tradeType: 'JSAPI',
      outTradeNo: that.data.orderList[index].outTradeNo,
      spbillCreateIp: '218.16.63.137'
    }
    wx.request({
      url: `${app.globalData.testApiUrl}/api/wx/pay/createOrder`,
      method: 'POST',
      // data: {
      //   appid: app.globalData.appId,
      //   openid: app.globalData.openId,
      //   nonceStr: app.randomWord(),
      //   notifyUrl: `${app.globalData.testApiUrl}/api/wx/pay/notify/order`,
      //   body: that.data.orderList[index].remark,
      //   totalFee: that.data.orderList[index].pay * 100,
      //   tradeType: 'JSAPI',
      //   outTradeNo: that.data.orderList[index].outTradeNo,
      //   spbillCreateIp: '218.16.63.137'
      //   // spbillCreateIp: app.globalData.ip
      // },
      data: params,
      success: (res) => {
        wx.requestPayment({
          provider: 'wxpay',
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.packageValue,
          signType: res.data.signType,
          paySign: res.data.paySign,
          appId: app.globalData.openId,
          success: res => {
            console.log(res)
            wx.showToast({
              title: '支付成功',
              duration: 2000
            })
            setTimeout(() => {
              // wx.redirectTo({
              //   url: '/pages/index/index',
              // })
              this.getAllddList()
            }, 2000)
          },
          fail: (error) => {
            // wx.redirectTo({
            //   url: '/pages/index/index',
            // })
          },
          complete: function (e) {
            app.errorDate.backData = e.data
            app.errorDate.statusCode = e.statusCode
            app.errorDate.route = 'index'
            app.errorDate.interface = '/api/wx/pay/notify/order'
            app.setLogCloud()
            wx.hideLoading()
          }
        })
      },
      fail: (error) => {

      },
      complete: function (e) {
        app.errorDate.params = params
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/pay/createOrder'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  orderHurry: function () {},
  orderScan: function (e) {
    this.setData({
      quickScan: true,
      outCheckNo: e.currentTarget.dataset.checkno
    })
    let size = {}
    size.w = wx.getSystemInfoSync().windowWidth / 750 * 600
    size.h = size.w
    drawQrcode({
      width: size.w,
      height: size.h,
      canvasId: 'myQrcode',
      // ctx: wx.createCanvasContext('myQrcode'),
      text: e.currentTarget.dataset.checkno
      // v1.0.0+版本支持在二维码上绘制图片
    })
  },
  closeQuick: function () {
    this.setData({
      quickScan: false
    })
  },
  // 跳转到营地详情页面
  tocampDetails: function (params) {
    var campId = params.currentTarget.dataset.campid
    wx.navigateTo({
      url: `../campDetails/campDetails?campId=${campId}`
    })
   },
  //  订单搜索框
  searchInputChange (e) {
    let bool = false
    bool = e.detail.value !== '' ? true : false
    this.setData({
      searchIconToggle: bool,
      searchVal: e.detail.value
    })
  },
  // 清除搜索框内容
  clearselect () {
    this.setData({
      searchVal: ''
    })
  },
  // 订单查询界面的查询按钮
  selectbtn () {
    // 通过验证码查询订单信息
    var code = this.data.searchVal
    // 调用查询接口
    wx.request({
      url: `${app.globalData.testApiUrl}/api/wx/camp/orderCheck`,
      data: {
        checkNo: this.data.searchVal
      },
      success: (res) => {
        console.log(res)
        if (res.tentTypeId) {
          this.setData({
            orderCheck: true,
            ifOrderCheckEmpty: false
          })
          this.getCampTypeOf(res.data.campId, res.data.tentTypeId)
        } else {
          this.setData({
            orderCheck: false,
            ifOrderCheckEmpty: false
          })
        }
        // this.setData({
        //   orderCheck: true,
        //   arrlist: [
        //     { id: 0, img: "../../img/qishou.png", name: "编号", num: "0", price: "31", color: "绿", message2: "月租33", message3: ''},
        //     { id: 1, img: "../../img/qishou.png", name: "编号", num: "0", price: "51", color: "红", message2: "月租30", message3: '' },
        //     { id: 2, img: "../../img/qishou.png", name: "编号", num: "0", price: "71", color: "黄", message2: "月租20", message3: ''},
        //     { id: 3, img: "../../img/qishou.png", name: "编号", num: "0", price: "91", color: "黑", message2: "月租40", message3: ''},
        //     { id: 5, img: "../../img/qishou.png", name: "篝火晚会", num: "0", price: "51", message: "交际之花", message2: "", message3: ''},
        //     { id: 6, img: "../../img/qishou.png", name: "峡谷蹦极", num: "0", price: "51", message: "惊险刺激", message2: "", message3: '' },
        //   ]
        // })
      },
      fail: (error) => {
        console.log(error)
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/camp/orderCheck'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  //  跳转到订单详情页面
  toscanCodePage: function() {
    var that = this
    wx.scanCode({
      success(res) {
        console.log(res.result)
        that.setData({
          userPageType: 2
        })
        wx.request({
          url: `${app.globalData.testApiUrl}/api/wx/camp/orderCheck`,
          data: {
            checkNo: res.result
          },
          success: (res) => {
            console.log(res)
            if (res.tentTypeId) {
              this.setData({
                orderCheck: true,
                ifOrderCheckEmpty: false
              })
              this.getCampTypeOf(res.data.campId, res.data.tentTypeId)
            } else {
              this.setData({
                orderCheck: false,
                ifOrderCheckEmpty: false
              })
            }
            // this.setData({
            //   orderCheck: true,
            //   arrlist: [
            //     { id: 0, img: "../../img/qishou.png", name: "编号", num: "0", price: "31", color: "绿", message2: "月租33", message3: ''},
            //     { id: 1, img: "../../img/qishou.png", name: "编号", num: "0", price: "51", color: "红", message2: "月租30", message3: '' },
            //     { id: 2, img: "../../img/qishou.png", name: "编号", num: "0", price: "71", color: "黄", message2: "月租20", message3: ''},
            //     { id: 3, img: "../../img/qishou.png", name: "编号", num: "0", price: "91", color: "黑", message2: "月租40", message3: ''},
            //     { id: 5, img: "../../img/qishou.png", name: "篝火晚会", num: "0", price: "51", message: "交际之花", message2: "", message3: ''},
            //     { id: 6, img: "../../img/qishou.png", name: "峡谷蹦极", num: "0", price: "51", message: "惊险刺激", message2: "", message3: '' },
            //   ]
            // })
          },
          fail: (error) => {
            console.log(error)
          },
          complete: function (e) {
            app.errorDate.backData = e.data
            app.errorDate.statusCode = e.statusCode
            app.errorDate.route = 'index'
            app.errorDate.interface = '/api/wx/camp/orderCheck'
            app.setLogCloud()
            wx.hideLoading()
          }
        })
      }
    })
  },
  // 帐篷类型获取帐篷
  getCampTypeOf (campId, tentTypeId) {
    wx.request({
      url: `${app.globalData.testApiUrl}/api/wx/camp/listTent`,
      data: {
        campId: campId,
        tentTypeId: tentTypeId
      },
      success: (res) => {
        console.log(res)
        this.setData({
          arrlist: res.data
        })
      },
      fail: (error) => {
        console.log(error)
      },
      complete: function (e) {
        app.errorDate.backData = e.data
        app.errorDate.statusCode = e.statusCode
        app.errorDate.route = 'index'
        app.errorDate.interface = '/api/wx/camp/listTent'
        app.setLogCloud()
        wx.hideLoading()
      }
    })
  },
  // 办理入住
  toChechIn: function(e) {
    // 获取帐篷的编号或者ID

    // 调用弹框
    wx.showModal({
      title: '',
      content: '是否选定该帐篷办理入住',
      success(res) {
        if (res.confirm) {
          // 调用入住接口
          console.log('用户点击确定')
          wx.request({
            url: `${app.globalData.testApiUrl}/api/wx/camp/selectTent`,
            data: {
              checkNo: this.data.checkNo,
              tentId: e.currentTarget.dataset.tentId
            },
            success: (res) => {
              console.log(res)
            },
            fail: (error) => {
              console.log(error)
            },
            complete: function (e) {
              app.errorDate.backData = e.data
              app.errorDate.statusCode = e.statusCode
              app.errorDate.route = 'index'
              app.errorDate.interface = '/api/wx/camp/selectTent'
              app.setLogCloud()
              wx.hideLoading()
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 关于我们
  aboutUs: () => {
    wx.navigateTo({
      url: '/pages/aboutUs/aboutUs',
    })
  },
  // 意见反馈
  adviceFeedback: () => {
    wx.navigateTo({
      url: '/pages/adviceBack/adviceBack',
    })
  }
})
